
#include "desktop.h"
#include "desktop_data.h"
#include "desktop_udp_data.h"
#include "desktop_test.h"
#include <errno.h>
#include <sys/time.h>
#include <sys/ioctl.h>


int AWTIOT_Init(cfg_pu_t* pu);
int AWTIOT_Run(cfg_pu_t* pu);
int AWTIOT_Msg(cfg_pu_t* pu, msg_t *msg);

dlmodule_t AWTIOT = {
    AWTIOT_Init,
    AWTIOT_Run,
    AWTIOT_Msg
};

void *broadcast(void *param)
{
    cfg_pu_t *pu = (cfg_pu_t *)param;
    desktop_udp_run(pu);
}

void *autoCheckThread(void *param)
{
    cfg_pu_t *pu = (cfg_pu_t *)param;
    desktop_test_run(pu);
}

int AWTIOT_Init(cfg_pu_t *pu)
{
    UTIL_LOG_I(pu, "init");

    pu_param_t *param = (pu_param_t *)malloc(sizeof(pu_param_t));
    param->debug = 1;
    param->cnt = 0;
    rt_slist_init(&param->socket60000);
    rt_slist_init(&param->socket60003);
    pthread_mutex_init(&param->lock, NULL);
    pu->hanlder.param = param;
    /*
        pthread_t thread;
        int err = pthread_create(&thread, NULL, broadcast, pu);
        if (err)
        {
            UTIL_LOG_E(pu, "Failed to create udp thread!");
        }
        pthread_detach(thread);
    */

    pthread_t thread2; // autoCheck
    int err = pthread_create(&thread2, NULL, autoCheckThread, pu);
    if (err)
    {
        UTIL_LOG_E(pu, "Failed to create autoCheck thread!");
    }
    pthread_detach(thread2);

    return 0;
}

int AWTIOT_Run(cfg_pu_t *pu)
{
    UTIL_LOG_I(pu, "running!");
    desktop_run(pu);
}

int AWTIOT_Msg(cfg_pu_t *pu, msg_t *msg)
{

    return 0;
}
